package com.gy.ai.agent.core;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author LGY
 * Create by 2025/6/16 16:17
 */
@Data
public class BaseResponse implements Serializable {

    /**
     * 返回的源数据
     */
    private String sourceData;

    /**
     * 是否是流数据
     */
    private boolean stream;

    public BaseResponse() {
    }

    public BaseResponse(String sourceData) {
        this.sourceData = sourceData;
    }

    public BaseResponse(String sourceData, boolean stream) {
        this.sourceData = sourceData;
        this.stream = stream;
    }

    /**
     * 将元数据解析为指定对象
     *
     * @param clazz 目标对象
     * @return
     */
    public <T extends BaseResponse> T getResponse(Class<T> clazz) {
        if (StringUtils.isBlank(sourceData) || Objects.equals(sourceData.trim(), "[DONE]")) {
            return null;
        }
        return JSON.parseObject(this.sourceData, clazz);
    }
}
